// import type { WorkflowManager } from './manager';
import { FlowPlayer } from './player';

export interface FlowRecord {
	player: FlowPlayer;
	timestamp: number;
	/**
	 * Indicate which data copy are used by this record.
	 */
	dataVersion: number;
	nodeId: string;
	/**
	 * A zero based value, indicate repeatly appeared count of the node.
	 */
	nodeAppearId: number;
	actionId?: string;
	opinion?: string;
	nextNodes: { id: string; appearId: number }[];
}

export type FlowVariables = Record<string, unknown>;

export type CurrentNode = { nodeId: string; appearId: number };

export interface FlowInstanceInfo {
	/**
	 * Workflow instance ID
	 */
	readonly id: string;
	/**
	 * Workflow ID
	 */
	readonly flowId: string;
	/**
	 * Record which version of workflow definition are used.
	 */
	readonly flowVersion: string;
	/**
	 * Any instance state change will incremently this value.
	 */
	readonly version: number;
	/**
	 * Indicate whether the flow instance are finished. Workflow will be finished when it go into ending node or aborted manually.
	 */
	readonly finished: boolean;
	/**
	 * If workflow are aborted the reason should be provided.
	 */
	readonly abortReason: string | undefined;
	/**
	 * Workflow data may changed by some node processing, every data copy should be recorded, if data are changed 'dataVersion' should be incremented by 1.
	 */
	readonly dataVersion: number;
	/**
	 * Workflow data
	 */
	readonly data: unknown;
	/**
	 * Workflow instance may use some extra variables to support flow work correctly.
	 */
	readonly variables: FlowVariables;
	/**
	 * record the invoked count of each node.
	 */
	readonly nodeAppearId: Record<string, number>;
	/**
	 * record node's variables, key is <nodeId-appearId>
	 */
	readonly nodeVariables: Record<string, FlowVariables>;
	/**
	 * Currently process nodes
	 */
	readonly currentNodes: CurrentNode[];
	/**
	 * Approval history
	 */
	readonly records: FlowRecord[];
}

export interface FlowInstance extends FlowInstanceInfo {
	// manager: WorkflowManager;
	/**
	 * Workflow instance ID
	 */
	id: string;
	/**
	 * Workflow ID
	 */
	flowId: string;
	/**
	 * Record which version of workflow definition are used.
	 */
	flowVersion: string;
	/**
	 * Any instance state change will incremently this value.
	 */
	version: number;
	/**
	 * Indicate whether the flow instance are finished. Workflow will be finished when it go into ending node or aborted manually.
	 */
	finished: boolean;
	/**
	 * If workflow are aborted the reason should be provided.
	 */
	abortReason: string | undefined;
	/**
	 * Workflow data may changed by some node processing, every data copy should be recorded, if data are changed 'dataVersion' should be incremented by 1.
	 */
	dataVersion: number;
	/**
	 * Workflow data
	 */
	data: unknown;
	/**
	 * Workflow instance variables.
	 */
	variables: FlowVariables;
	/**
	 * record the invoked count of each node.
	 */
	nodeAppearId: Record<string, number>;
	/**
	 * record node's variables, key is <nodeId-appearId>
	 */
	nodeVariables: Record<string, FlowVariables>;
	/**
	 * Currently process nodes
	 */
	currentNodes: CurrentNode[];
	/**
	 * Approval history
	 */
	records: FlowRecord[];
}
